package org.me.app;



import org.me.app.instructor.InstructorProfileActivity;
import org.me.app.student.StudentProfileActivity;
import org.me.bean.InstructorData;
import org.me.bean.StudentData;
import org.me.services.WSlib;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	public static final String Log_Tag = "LoginActivity";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		Button submit = (Button) findViewById(R.id.btn_submit);
		submit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Intent data = new Intent();
				EditText txt_username = (EditText) findViewById(R.id.txt_username);
				EditText txt_pass = (EditText) findViewById(R.id.txt_password);
				WSlib service = new WSlib();
				String results = "";

				StudentData stuData = new StudentData();
				InstructorData instData = new InstructorData();

				String username = txt_username.getText().toString();
				String pass = txt_pass.getText().toString();

				try {
					String prefix = username.substring(0, 2);
					
					if (prefix.equalsIgnoreCase("IT")) {
						
						stuData = service.getStudentData(username, pass);
						if (stuData == null) {
							Toast.makeText(getApplicationContext(),
									"Wrong UserName or PassWord, Try again",
									Toast.LENGTH_SHORT).show();
						} else {
							Intent i = new Intent(getApplicationContext(),
									org.me.app.student.StudentProfileActivity.class);
							i.putExtra("org.me.bean.StudentData", stuData);
							startActivity(i);
						}
						
					} else if (prefix.equalsIgnoreCase("IU")) {
						
						instData = service.getInstructorData(username, pass);							
						if (instData == null) {
							
							Toast.makeText(getApplicationContext(),
									"Wrong UserName or PassWord, Try again",
									Toast.LENGTH_SHORT).show();
							
						} else {
							
							Intent i = new Intent(getApplicationContext(),
									org.me.app.instructor.InstructorProfileActivity.class);
							i.putExtra("org.me.bean.InstructorData", instData);
							startActivity(i);
							
						}
						
						
					}else{
						Toast.makeText(getApplicationContext(),
								"Wrong UserName or PassWord, Try again",
								Toast.LENGTH_SHORT).show();
					}
										
				} catch (Exception e) {
					Log.e(Log_Tag, e.toString());
				}
			}
		});

	}

}
